from django import forms
from captcha.fields import CaptchaField
from . import models


def get_editor(edit_type):
    edit_info = models.Editor.objects.get(type="key")
    if edit_type == "theme":
        return edit_info.theme
    elif edit_type == "title":
        return edit_info.title
    elif edit_type == "content":
        return edit_info.content
    elif edit_type == "status":
        return edit_info.status
    elif edit_type == "important_level":
        return edit_info.important_level
    elif edit_type == "alert_mode":
        return edit_info.alert_mode
    elif edit_type == "start_date":
        return edit_info.start_date
    else:
        return edit_info.end_date


class LoginForm(forms.Form):
    username = forms.CharField(label="用户名", max_length=18, min_length=2, required=True, error_messages=
    {
        'required': '用户名不能为空',
        'max_length': '用户名过长',
        'min_length': '用户名过短',
        'invalid': '格式输入有误'
    }, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "请输入您的用户名"}))
    password = forms.CharField(label="密码", max_length=18,
                               widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': "请输入您的密码"}),
                               min_length=6, required=True,
                               error_messages=
                               {
                                   'required': '密码不能为空',
                                   'max_length': '密码过长',
                                   'min_length': '密码过短',
                                   'invalid': '格式输入有误'
                               })
    captcha = CaptchaField(label='验证码')


class RegisterForm(forms.Form):
    username = forms.CharField(label='用户名', max_length=18,
                               widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "请输入您的用户名"}),
                               min_length=2, required=True, error_messages=
                               {
                                   'required': '用户名不能为空',
                                   'max_length': '用户名过长',
                                   'min_length': '用户名过短',
                                   'invalid': '格式输入有误'
                               })
    user_id = forms.CharField(label='学号', max_length=18,
                              widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "请输入您的学号"}),
                              min_length=2, required=True, error_messages=
                              {
                                  'required': '学号不能为空',
                                  'max_length': '学号过长',
                                  'min_length': '学号过短',
                                  'invalid': '格式输入有误'
                              })
    real_name = forms.CharField(label='真实姓名', max_length=18,
                                widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "请输入您的真实姓名"}),
                                min_length=2, required=True, error_messages=
                                {
                                    'required': '姓名不能为空',
                                    'max_length': '姓名过长',
                                    'min_length': '姓名过短',
                                    'invalid': '格式输入有误'
                                })
    password = forms.CharField(label='密码', max_length=18, min_length=6, required=True, error_messages=
    {
        'required': '密码不能为空',
        'max_length': '密码过长',
        'min_length': '密码过短',
        'invalid': '格式输入有误'
    },
                               widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': "请输入您的密码"}))
    password_confirm = forms.CharField(label='确认密码', max_length=256, widget=forms.PasswordInput(
        attrs={'class': 'form-control', 'placeholder': "请再输入一遍密码"}))
    email = forms.EmailField(label='电子邮件地址(选填)', required=False, min_length=3, max_length=18,
                             widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': "请输入您的电子邮箱地址"}))
    captcha = CaptchaField(label='图形验证码')


class ScheduleForm(forms.Form):
    content = forms.CharField(label='内容', max_length=2048, min_length=6, required=True, error_messages=
    {
        'required': '内容不能为空',
        'max_length': '内容过长',
        'min_length': '内容过短',
        'invalid': '格式输入有误'
    },
                              widget=forms.Textarea(attrs={'class': 'form-control', 'placeholder': "请输入内容"}))

    title = forms.CharField(label='标题', max_length=20, min_length=2, required=True, error_messages=
    {
        'required': '标题不能为空',
        'max_length': '标题过长',
        'min_length': '标题过短',
        'invalid': '格式输入有误'
    },
                            widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "请输入标题"}))

    start_date = forms.DateTimeField(label="开始时间", widget=forms.DateTimeInput(attrs={'class': 'form-control',
                                                                                     'placeholder': "请输入开始时间"}),
                                     required=True)
    end_date = forms.DateTimeField(label="结束时间", widget=forms.DateTimeInput(attrs={'class': 'form-control',
                                                                                   'placeholder': "请输入结束时间"}),
                                   required=True)
    important_level = forms.ChoiceField(choices=[("重要", "重要"), ("一般", "一般"), ("不重要", "不重要")], initial="一般",
                                        label="重要程度", required=True)
    theme = forms.ChoiceField(choices=[("学习", "学习"), ("工作", "工作"), ("社交", "社交"), ("锻炼", "锻炼"), ("娱乐", "娱乐")],
                              initial="学习",
                              label="主题", required=True)
    alert_mode = forms.ChoiceField(choices=[("短信提醒", "短信提醒"), ("邮箱提醒", "邮箱提醒"), ("电话提醒", "电话提醒")], initial="短信提醒",
                                   label="提醒方式", required=True)


class ControlForm(forms.Form):
    old_psw = forms.CharField(label='旧密码', max_length=18, min_length=6, required=True, error_messages=
    {
        'required': '密码不能为空',
        'max_length': '密码过长',
        'min_length': '密码过短',
        'invalid': '格式输入有误'
    },
                              widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': "请输入您的旧密码"}))
    new_psw = forms.CharField(label='新密码', max_length=18, min_length=6, required=True, error_messages=
    {
        'required': '密码不能为空',
        'max_length': '密码过长',
        'min_length': '密码过短',
        'invalid': '格式输入有误'
    },
                              widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': "请输入您的新密码"}))

    new_psw_confirm = forms.CharField(label='确认密码', max_length=18, min_length=6, required=True, error_messages=
    {
        'required': '密码不能为空',
        'max_length': '密码过长',
        'min_length': '密码过短',
        'invalid': '格式输入有误'
    },
                                      widget=forms.PasswordInput(
                                          attrs={'class': 'form-control', 'placeholder': "确认密码"}))


class EditScheduleForm(forms.Form):
    content = forms.CharField(label='内容', max_length=2048, min_length=6, required=True, error_messages=
    {
        'required': '内容不能为空',
        'max_length': '内容过长',
        'min_length': '内容过短',
        'invalid': '格式输入有误'
    },
                              widget=forms.Textarea(attrs={'class': 'form-control'}), initial=get_editor("content"))

    title = forms.CharField(label='标题', max_length=20, min_length=2, required=True, error_messages=
    {
        'required': '标题不能为空',
        'max_length': '标题过长',
        'min_length': '标题过短',
        'invalid': '格式输入有误'
    },
                            widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "请输入标题"}),
                            initial=get_editor('title'))

    start_date = forms.DateTimeField(label="开始时间", widget=forms.DateTimeInput(attrs={'class': 'form-control',
                                                                                     'placeholder': "请输入开始时间"}),
                                     required=True, initial=get_editor('start_date'))
    end_date = forms.DateTimeField(label="结束时间", widget=forms.DateTimeInput(attrs={'class': 'form-control',
                                                                                   'placeholder': "请输入结束时间"}),
                                   required=True, initial=get_editor('end_date'))
    important_level = forms.ChoiceField(choices=[("重要", "重要"), ("一般", "一般"), ("不重要", "不重要")],
                                        initial=get_editor('important_level'),
                                        label="重要程度", required=True)
    theme = forms.ChoiceField(choices=[("学习", "学习"), ("工作", "工作"), ("社交", "社交"), ("锻炼", "锻炼"), ("娱乐", "娱乐")],
                              initial=get_editor('theme'),
                              label="主题", required=True)
    alert_mode = forms.ChoiceField(choices=[("短信提醒", "短信提醒"), ("邮箱提醒", "邮箱提醒"), ("电话提醒", "电话提醒")],
                                   initial=get_editor('alert_mode'),
                                   label="提醒方式", required=True)

    # status = forms.ChoiceField(choices=[("已完成", "已完成"), ("未完成", "未完成")], initial=get_editor('status'),
    #                            label="完成状态", required=True)
